package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.PasswordErrorException;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2023年09月19日 11:18
 */
public class MainController {

    // 当前登录进来的用户
    private User user;
    private Scanner sc = new Scanner(System.in);
    // 当用户成功登录进来之后,给与用户一个购物车
    private Cart cart = new Cart();
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
    private UserService userService = (UserService) ObjectFactory.getObject("userService");

    public MainController(User user) {
        this.user = user;
    }

    public void showMenu() {
        System.out.println("***************欢迎成功登录网络商城***************");
        System.out.print("请选择服务(1.加入购物车 2.查看购物车 3.我的订单 4.修改密码 5.退出登录):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            case 3:
                findMyOrders();
                break;
            case 4:
                modifyPassword();
                break;
            case 5:
                logout();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入!");
                showMenu();
                break;
        }
    }

    public void addCart() {
        // 先列出需要加入购物车中所有商品信息
        List<Product> products = productService.findAll();
        if (!products.isEmpty()) {
            System.out.println("--------------------------------------");
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
            System.out.println("--------------------------------------");
        }
        System.out.print("请输入需要加入购物车中的商品编号:");
        String id = sc.next();

        try {
            cartService.addCart(cart, id);
            System.out.println("提示:加入成功");
        } catch (UserNotLoginException e) {
            System.out.println("错误:加入失败,失败原因:" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:加入失败,失败原因:" + e.getMessage());
        } catch (ProductNotExistException e) {
            System.out.println("错误:加入失败,失败原因:" + e.getMessage());
        }
        showMenu();

    }

    public void showCart() {
        // 查看购物车
        System.out.println("编号\t\t商品名称\t\t数量\t\t总价");
        for (Item item : cart.getItems()) {
            System.out.println(item.getProduct().getId() + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
        }
        System.out.println("-------------------------------------------");
        System.out.println("购物车总价:" + cart.getPrice());
        System.out.println("-------------------------------------------");

        System.out.print("请选择服务(1.删除商品 2.修改商品数量 3.清空购物车 4.结算 5.返回主菜单):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                clean();
                break;
            case 4:
                createOrder();
                break;
            case 5:
                showMenu();
                break;
            default:
                System.out.println("您输入选项有误,请重新输入!");
                showCart();
                break;
        }
    }

    public void removeCart() {
        System.out.print("请输入需要删除的商品编号:");
        String id = sc.next();
        try {
            cartService.removeCart(cart, id);
            System.out.println("提示:恭喜您,删除成功!");
        } catch (UserNotLoginException e) {
            System.out.println("错误:对不起,删除失败,失败原因:" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:对不起,删除失败,失败原因:" + e.getMessage());
        }
        showCart();
    }

    public void modifyCart() {
        System.out.print("请输入需要修改的商品编号:");
        String id = sc.next();
        System.out.print("请输入修改后的数量:");
        String num = sc.next();

        try {
            cartService.modifyCart(cart, id, num);
            System.out.println("提示:恭喜您,修改成功!");
        } catch (UserNotLoginException e) {
            System.out.println("错误:对不起,修改失败,失败原因:" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:对不起,修改失败,失败原因:" + e.getMessage());
        }
        showCart();
    }

    public void clean() {
        cart = new Cart();
        showCart();
    }

    public void createOrder() {
        try {
            String no = orderService.createOrder(cart, user);
            // 结算成功之后,清空当前结算的购物车
            cart = new Cart();
            System.out.println("提示:结算成功,订单号为:" + no);
        } catch (UserNotLoginException e) {
            System.out.println("错误:结算失败,失败原因:" + e.getMessage());
        }
        showMenu();
    }

    /**
     * 查询当前用户所有的订单信息
     */
    public void findMyOrders() {
        try {
            List<Order> orders = orderService.findMyOrders(user);
            System.out.println("编号\t\t订单号\t\t总价");
            for (Order order : orders) {
                System.out.println(order.getId() + "\t\t" + order.getNo() + "\t\t" + order.getPrice());
            }
            System.out.print("请选择服务(1.查看订单详情 2.返回主菜单):");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    findOrderDetail();
                    break;
                case 2:
                    showMenu();
                    break;
                default:
                    System.out.println("您输入的选项有误,请重新输入!");
                    findMyOrders();
                    break;
            }
        } catch (UserNotLoginException e) {
            System.out.println("错误:请先登录");
            new UserController().showMenu();
        }
    }

    public void findOrderDetail() {
        // 详情控制台展示下面效果,且涉及到的所有数据通过一条sql语句获取
//        用户名:xxx
//        手机号:xxx
//        订单号:xxx
//        ------------------------------------
//        序号  商品名   数量     总价
//        1	    aaa		2		5.0
//        2	    bbb		1		4.0
//        3	    ccc		3		12.0
//        -----------------------------------
//        共:21.0元	总计:3条
        System.out.print("请输入需要查看明细的订单编号:");
        String id = sc.next();
        try {
            Order order = orderService.findOrderDetail(id);
            System.out.println("用户名:" + order.getUser().getUsername());
            System.out.println("手机号:" + order.getUser().getPhone());
            System.out.println("订单号:" + order.getNo());
            System.out.println("-------------------------------------");
            System.out.println("序号\t\t商品名称\t\t数量\t\t总价");
            List<Item> items = order.getItems();
            for (int i = 0; i < items.size(); i++) {
                Item item = items.get(i);
                System.out.println((i + 1) + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
            }
            System.out.println("-------------------------------------");
            System.out.println("共:" + order.getPrice() + "元\t\t,总计:" + order.getCount() + "条");
            System.out.print("请选择服务(1.返回我的订单 2.返回主菜单):");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    findMyOrders();
                    break;
                case 2:
                    showMenu();
                    break;
                default:
                    System.out.println("您输入的选项有误,请重新输入!");
                    findOrderDetail();
                    break;
            }
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:查看失败,失败原因:" + e.getMessage());
            findMyOrders();
        }
    }

    public void modifyPassword() {
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        System.out.print("请输入新密码:");
        String newPassword = sc.next();
        System.out.print("请再次输入新密码:");
        String newPassword2 = sc.next();

        try {
            userService.modifyPassword(user, oldPassword, newPassword, newPassword2);
            System.out.println("提示:恭喜您,密码修改成功!");
            new UserController().showMenu();
        } catch (UserNotLoginException e) {
            System.out.println("错误:对不起,密码修改失败,失败原因:" + e.getMessage());
            showMenu();
        } catch (PasswordErrorException e) {
            System.out.println("错误:对不起,密码修改失败,失败原因:" + e.getMessage());
            showMenu();
        }
    }

    public void logout() {
        new UserController().showMenu();
    }

}
